package com.syzsn.sk8.service.impl;

import com.syzsn.common.enums.WorldCupGroupName;
import com.syzsn.sk8.model.SySK8WorldCupGroupRecordModel;
import com.syzsn.sk8.repository.SySK8WorldCupDivisionRepository;
import com.syzsn.sk8.service.SySK8WorldCupService;
import com.syzsn.sk8.entity.SySK8WorldCupDivision;
import com.syzsn.sk8.entity.SySK8WorldCupGroupRecord;
import com.syzsn.sk8.enums.SK8Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SySK8WorldCupServiceImpl implements SySK8WorldCupService {

    private SySK8WorldCupGroupRecordModel groupRecordModel1;
    private SySK8WorldCupGroupRecordModel groupRecordModel2;
    private SySK8WorldCupGroupRecordModel groupRecordModel3;
    private SySK8WorldCupGroupRecordModel groupRecordModel4;
    private SySK8WorldCupGroupRecordModel groupRecordModel5;
    private SySK8WorldCupGroupRecordModel groupRecordModel6;
    private SySK8WorldCupGroupRecordModel groupRecordModel7;
    private SySK8WorldCupGroupRecordModel groupRecordModel8;
    private SySK8WorldCupGroupRecordModel groupRecordModel9;
    private SySK8WorldCupGroupRecordModel groupRecordModel10;
    private SySK8WorldCupGroupRecordModel groupRecordModel11;
    private SySK8WorldCupGroupRecordModel groupRecordModel12;

    @Autowired
    private SySK8WorldCupDivisionRepository sySK8WorldCupDivisionRepository;

    @Override
    public List<SySK8WorldCupGroupRecordModel> getGroupRecordModelList(List<SySK8WorldCupGroupRecord> sySK8WorldCupGroupRecordList) {
        List<SySK8WorldCupGroupRecordModel> groupRecordModelList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(sySK8WorldCupGroupRecordList)){
            initGroupRecordModel();
            int groupNum = 1;
            int recordNum = 1;
            String groupName = WorldCupGroupName.A.name();
            for(SySK8WorldCupGroupRecord groupRecord : sySK8WorldCupGroupRecordList){
                if(groupName.equals(groupRecord.getGroupName().name())){
                    if(recordNum == 6){
                        recordNum = 1;
                    }else{
                        recordNum++;
                    }
                }else{
                    groupName = groupRecord.getGroupName().name();
                    groupNum++;
                }
                setGroupRecord(groupNum, recordNum, groupRecord);
            }
            addGroupRecordModelList(groupRecordModelList);
        }
        return groupRecordModelList;
    }

    private void addGroupRecordModelList(List<SySK8WorldCupGroupRecordModel> groupRecordModelList) {
        groupRecordModelList.add(groupRecordModel1);
        groupRecordModelList.add(groupRecordModel2);
        groupRecordModelList.add(groupRecordModel3);
        groupRecordModelList.add(groupRecordModel4);
        groupRecordModelList.add(groupRecordModel5);
        groupRecordModelList.add(groupRecordModel6);
        groupRecordModelList.add(groupRecordModel7);
        groupRecordModelList.add(groupRecordModel8);
        groupRecordModelList.add(groupRecordModel9);
        groupRecordModelList.add(groupRecordModel10);
        groupRecordModelList.add(groupRecordModel11);
        groupRecordModelList.add(groupRecordModel12);
    }

    private void initGroupRecordModel() {
        groupRecordModel1 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel2 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel3 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel4 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel5 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel6 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel7 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel8 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel9 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel10 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel11 = new SySK8WorldCupGroupRecordModel();
        groupRecordModel12 = new SySK8WorldCupGroupRecordModel();
    }

    private void setGroupRecord(int groupNum, int recordNum, SySK8WorldCupGroupRecord groupRecord) {
        // 1111
        if(groupNum == 1 && recordNum == 1){
            groupRecordModel1.setGroupRecord1(groupRecord);
        }
        if(groupNum == 1 && recordNum == 2){
            groupRecordModel1.setGroupRecord2(groupRecord);
        }
        if(groupNum == 2 && recordNum == 1){
            groupRecordModel1.setGroupRecord3(groupRecord);
        }
        if(groupNum == 2 && recordNum == 2){
            groupRecordModel1.setGroupRecord4(groupRecord);
        }
        // 2222
        if(groupNum == 1 && recordNum == 3){
            groupRecordModel2.setGroupRecord1(groupRecord);
        }
        if(groupNum == 1 && recordNum == 4){
            groupRecordModel2.setGroupRecord2(groupRecord);
        }
        if(groupNum == 2 && recordNum == 3){
            groupRecordModel2.setGroupRecord3(groupRecord);
        }
        if(groupNum == 2 && recordNum == 4){
            groupRecordModel2.setGroupRecord4(groupRecord);
        }
        // 3333
        if(groupNum == 1 && recordNum == 5){
            groupRecordModel3.setGroupRecord1(groupRecord);
        }
        if(groupNum == 1 && recordNum == 6){
            groupRecordModel3.setGroupRecord2(groupRecord);
        }
        if(groupNum == 2 && recordNum == 5){
            groupRecordModel3.setGroupRecord3(groupRecord);
        }
        if(groupNum == 2 && recordNum == 6){
            groupRecordModel3.setGroupRecord4(groupRecord);
        }
        // 4444
        if(groupNum == 3 && recordNum == 1){
            groupRecordModel4.setGroupRecord1(groupRecord);
        }
        if(groupNum == 3 && recordNum == 2){
            groupRecordModel4.setGroupRecord2(groupRecord);
        }
        if(groupNum == 4 && recordNum == 1){
            groupRecordModel4.setGroupRecord3(groupRecord);
        }
        if(groupNum == 4 && recordNum == 2){
            groupRecordModel4.setGroupRecord4(groupRecord);
        }
        // 5555
        if(groupNum == 3 && recordNum == 3){
            groupRecordModel5.setGroupRecord1(groupRecord);
        }
        if(groupNum == 3 && recordNum == 4){
            groupRecordModel5.setGroupRecord2(groupRecord);
        }
        if(groupNum == 4 && recordNum == 3){
            groupRecordModel5.setGroupRecord3(groupRecord);
        }
        if(groupNum == 4 && recordNum == 4){
            groupRecordModel5.setGroupRecord4(groupRecord);
        }
        // 6666
        if(groupNum == 3 && recordNum == 5){
            groupRecordModel6.setGroupRecord1(groupRecord);
        }
        if(groupNum == 3 && recordNum == 6){
            groupRecordModel6.setGroupRecord2(groupRecord);
        }
        if(groupNum == 4 && recordNum == 5){
            groupRecordModel6.setGroupRecord3(groupRecord);
        }
        if(groupNum == 4 && recordNum == 6){
            groupRecordModel6.setGroupRecord4(groupRecord);
        }
        // 7777
        if(groupNum == 5 && recordNum == 1){
            groupRecordModel7.setGroupRecord1(groupRecord);
        }
        if(groupNum == 5 && recordNum == 2){
            groupRecordModel7.setGroupRecord2(groupRecord);
        }
        if(groupNum == 6 && recordNum == 1){
            groupRecordModel7.setGroupRecord3(groupRecord);
        }
        if(groupNum == 6 && recordNum == 2){
            groupRecordModel7.setGroupRecord4(groupRecord);
        }
        // 8888
        if(groupNum == 5 && recordNum == 3){
            groupRecordModel8.setGroupRecord1(groupRecord);
        }
        if(groupNum == 5 && recordNum == 4){
            groupRecordModel8.setGroupRecord2(groupRecord);
        }
        if(groupNum == 6 && recordNum == 3){
            groupRecordModel8.setGroupRecord3(groupRecord);
        }
        if(groupNum == 6 && recordNum == 4){
            groupRecordModel8.setGroupRecord4(groupRecord);
        }
        // 9999
        if(groupNum == 5 && recordNum == 5){
            groupRecordModel9.setGroupRecord1(groupRecord);
        }
        if(groupNum == 5 && recordNum == 6){
            groupRecordModel9.setGroupRecord2(groupRecord);
        }
        if(groupNum == 6 && recordNum == 5){
            groupRecordModel9.setGroupRecord3(groupRecord);
        }
        if(groupNum == 6 && recordNum == 6){
            groupRecordModel9.setGroupRecord4(groupRecord);
        }
        // 10 10 10 10
        if(groupNum == 7 && recordNum == 1){
            groupRecordModel10.setGroupRecord1(groupRecord);
        }
        if(groupNum == 7 && recordNum == 2){
            groupRecordModel10.setGroupRecord2(groupRecord);
        }
        if(groupNum == 8 && recordNum == 1){
            groupRecordModel10.setGroupRecord3(groupRecord);
        }
        if(groupNum == 8 && recordNum == 2){
            groupRecordModel10.setGroupRecord4(groupRecord);
        }
        // 11 11 11 11
        if(groupNum == 7 && recordNum == 3){
            groupRecordModel11.setGroupRecord1(groupRecord);
        }
        if(groupNum == 7 && recordNum == 4){
            groupRecordModel11.setGroupRecord2(groupRecord);
        }
        if(groupNum == 8 && recordNum == 3){
            groupRecordModel11.setGroupRecord3(groupRecord);
        }
        if(groupNum == 8 && recordNum == 4){
            groupRecordModel11.setGroupRecord4(groupRecord);
        }
        // 12 12 12 12
        if(groupNum == 7 && recordNum == 5){
            groupRecordModel12.setGroupRecord1(groupRecord);
        }
        if(groupNum == 7 && recordNum == 6){
            groupRecordModel12.setGroupRecord2(groupRecord);
        }
        if(groupNum == 8 && recordNum == 5){
            groupRecordModel12.setGroupRecord3(groupRecord);
        }
        if(groupNum == 8 && recordNum == 6){
            groupRecordModel12.setGroupRecord4(groupRecord);
        }
    }

    @Override
    public Map<String, List<String>> getCountryListByWcId(Integer wcId) {
        List<SySK8WorldCupDivision> sk8WorldCupDivisionList = sySK8WorldCupDivisionRepository.findListByWcIdForNoDivision(wcId);
        Map<String, List<String>> map = new HashMap<>();
        if(!CollectionUtils.isEmpty(sk8WorldCupDivisionList)) {
            for (SK8Type sk8Type : SK8Type.values()) {
                List<String> list = new ArrayList<>();
                for(SySK8WorldCupDivision division : sk8WorldCupDivisionList){
                    if(sk8Type.equals(division.getSk8Type())){
                        list.add("/"+division.getCountry().getContinentType().name()+"/"+division.getCountry().getEnName()+"_"+division.getCountry().getCnName());
                    }
                }
                map.put(sk8Type.getValue(), list);
            }
        }
        return map;
    }
}
